﻿using UnityEngine;
using System.Collections;
using cn.sharesdk.unity3d;

public class Login : MonoBehaviour {

    public ShareSDK ssdk;
    public PlatformType m_type = PlatformType.SinaWeibo;

	// Use this for initialization
	void Start () {
        ssdk = gameObject.GetComponent<ShareSDK>();
        ssdk.authHandler = OnAuthResultHandler;
        ssdk.shareHandler = OnShareResultHandler;
        ssdk.showUserHandler = OnGetUserInfoResultHandler;
        ssdk.getFriendsHandler = OnGetFriendsResultHandler;
        ssdk.followFriendHandler = OnFollowFriendResultHandler;

        if (ssdk.IsAuthorized(m_type))
        {
            Hashtable authInfo = ssdk.GetAuthInfo(m_type);
            SetInfo(m_type, authInfo);
        }
        else
        {
        }
	}

    private void OnFollowFriendResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        Hashtable authInfo = ssdk.GetAuthInfo(type);
        SetInfo(type, authInfo);
        print("share result :");
        print(MiniJSON.jsonEncode(authInfo));
    }

    private void OnGetFriendsResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        throw new System.NotImplementedException();
    }

    private void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        throw new System.NotImplementedException();
    }

    private void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        throw new System.NotImplementedException();
    }

    private void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        throw new System.NotImplementedException();
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    public void Authorize()
    {
        print(ssdk == null);

        ssdk.Authorize(PlatformType.SinaWeibo);
    }

    private void SetInfo(PlatformType type, Hashtable result)
    {
        string userName = "";
        string url = "";
        if (type == PlatformType.SinaWeibo)
        {
            userName = result["userName"].ToString();
            url = result["userIcon"].ToString();
        }
        
    }
}
